<?php

namespace App\Admin\Controllers;

use App\Models\Region;
use App\Models\Scene;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;

class SceneController extends Controller
{
    use HasResourceActions;

    /**
     * Index interface.
     *
     * @param Content $content
     * @return Content
     */
    public function index(Content $content)
    {
        return $content
            ->header('景点管理')
            ->description('景点')
            ->body($this->grid());
    }

    /**
     * Show interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function show($id, Content $content)
    {
        return $content
            ->header('Detail')
            ->description('description')
            ->body($this->detail($id));
    }

    /**
     * Edit interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function edit($id, Content $content)
    {
        return $content
            ->header('Edit')
            ->description('description')
            ->body($this->form()->edit($id));
    }

    /**
     * Create interface.
     *
     * @param Content $content
     * @return Content
     */
    public function create(Content $content)
    {
        return $content
            ->header('Create')
            ->description('description')
            ->body($this->form());
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Scene);

        $grid->id('ID');
        $grid->map()->display(function($map){
            return $map==1?'概览':'详细';
        });
        $grid->loc();
        $grid->title();
        $grid->area()->label();
        $grid->coverimage()->image('http://media.weijingguanjia.com/',36,36);
        $grid->bannerimage()->display(function ($images) {
            return $images;
        })->image('http://media.weijingguanjia.com/', 36, 36);
        $grid->recommended()->using([1 => '很差', '2' => '一般',3=>'良好',4=>'满意',5=>'非常满意'])->badge();
        $grid->columns('price','tag','iphone','address','lanter');
        $grid->rural()->using([
            0 => '一般',
            1 => '乡村旅游'
        ]);
        $grid->recommend()->switch([
            'on'  => ['value' => 1, 'text' => '推荐', 'color' => 'primary'],
            'off' => ['value' => 0, 'text' => '一般', 'color' => 'default'],
        ]);
        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Scene::findOrFail($id));

        $show->id('ID');
        $show->created_at('Created at');
        $show->updated_at('Updated at');

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Scene);
        $form->select('area','所属区域')->options(Region::all()->pluck('text','text'));
        $form->radio('map', '地图')->options(['1' => '概览', '2'=> '详细']);
        $form->text('loc', '手绘定位')->rules('required');
        $form->text('title','标题')->rules('required');
        $form->textarea('content','内容')->rules('required');
        $form->image('coverimage','封面图片')->uniqueName()->removable()->rules('required');
        $form->multipleImage('bannerimage','轮播图片')->uniqueName()->removable();
        $form->file('audiourl','音频')->uniqueName()->removable();
        $form->select('recommended','推荐指数')->options(['1' => '很差', '2'=> '一般', '3'=> '良好', '4'=> '满意', '5'=> '非常满意']);
        $form->number('price','门票价格');
        $form->tags('tag','标签')->rules('required');
        $form->text('iphone','电话');
        $form->text('address','地址');
        $form->text('lanter','经纬度')->rules('required');
        $form->textarea('introduction','景点简介');
        $form->textarea('transport','交通');
        $form->radio('rural')->options(['0' => '一般', '2'=> '乡村旅游']);;
        $form->switch('recommend');
        return $form;
    }
}
